﻿using System;
using System.Text;
using System.Collections.Generic;

namespace TestMiniAccess.AccessEntity
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Ruandao.MiniAccess;
    using Ruandao.MiniAccess.SqlServer;

    /// <summary>
    /// UnitTest1 的摘要说明
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 其他测试属性
        //
        // 您可以在编写测试时使用下列其他属性:
        //
        // 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) 
        // { 
        // }
        
        // 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // 在运行每个测试之前使用 TestInitialize 运行代码 
        [TestInitialize()]
        public void MyTestInitialize() 
        {
            //string dbProjectPath = this.TestContext.TestDir + "/../../DbTestMiniDac";
            //DbUtility.ResetTestData("TestMiniDac", dbProjectPath, "ResetData.bat");
        }
        
        // 在运行每个测试之后使用 TestCleanup 运行代码
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestMethod1()
        {
            //
            // TODO: 在此添加测试	逻辑
            //
        }

        [TestMethod]
        public void InsertEntityTest()
        {
            DataTypeEntity t1 = new DataTypeEntity();

            t1.BitField = true;
            t1.DateTimeField = DateTime.Now;
            t1.DecimalField = (decimal)1234.5678;
            t1.FloatField = (double)555.666;
            t1.GuidField = Guid.NewGuid();
            t1.ImageField = new byte[] { 1, 2, 3, 4, 5, 6 };
            t1.IntEnumField = IntEnum.Int1;
            t1.IntField = 88;
            t1.MoneyField = (decimal)88.88;
            t1.NTextField = "NTextField";
            t1.NVarCharEnumField = NVarCharEnum.NVarChar1;
            t1.NVarCharField = "NVarCharField";
            t1.RealField = (float)333.444;
            t1.TableId = 0;
            t1.VarBinaryField = new byte[] { 10, 20, 30, 40, 50, 60 };

            int rowcount = DataTypeEntity.GetAllEntities().Count;

            t1.Save();

            Assert.AreNotEqual(0, t1.TableId);
            Assert.AreEqual(rowcount + 1, DataTypeEntity.GetAllEntities().Count);

            DataTypeEntity t2 = DataTypeEntity.GetEntity(t1.TableId);
            Assert.AreEqual(t1.BitField, t2.BitField);
            Assert.AreEqual(t1.DateTimeField.ToLongDateString(), t2.DateTimeField.ToLongDateString());
            Assert.AreEqual(t1.DecimalField, t2.DecimalField);
            Assert.AreEqual(t1.FloatField, t2.FloatField, 0.01);
            Assert.AreEqual(t1.GuidField, t2.GuidField);
            Assert.AreEqual(t1.ImageField[2], t2.ImageField[2]);
            Assert.AreEqual(t1.IntEnumField, t2.IntEnumField);
            Assert.AreEqual(t1.IntField, t2.IntField);
            Assert.AreEqual(t1.MoneyField, t2.MoneyField);
            Assert.AreEqual(t1.NTextField, t2.NTextField);
            Assert.AreEqual(t1.NVarCharEnumField, t2.NVarCharEnumField);
            Assert.AreEqual(t1.NVarCharField, t2.NVarCharField);
            Assert.AreEqual(t1.RealField, t2.RealField);
            Assert.AreEqual(t1.TableId, t2.TableId);
            Assert.AreEqual(t1.VarBinaryField[3], t2.VarBinaryField[3]);

            DataTypeEntity.Delete(t1.TableId);
            Assert.AreEqual(rowcount, DataTypeEntity.GetAllEntities().Count);

        }


        /// <summary>
        ///UpdateEntity (T, params object[]) 的测试
        ///</summary>
        [TestMethod]
        public void UpdateEntityTest()
        {
            DataTypeEntity t1 = new DataTypeEntity();

            t1.BitField = true;
            t1.DateTimeField = DateTime.Now;
            t1.DecimalField = (decimal)1234.5678;
            t1.FloatField = (double)555.666;
            t1.GuidField = Guid.NewGuid();
            t1.ImageField = new byte[] { 1, 2, 3, 4, 5, 6 };
            t1.IntEnumField = IntEnum.Int1;
            t1.IntField = 88;
            t1.MoneyField = (decimal)88.88;
            t1.NTextField = "NTextField";
            t1.NVarCharEnumField = NVarCharEnum.NVarChar1;
            t1.NVarCharField = "NVarCharField";
            t1.RealField = (float)333.444;
            t1.TableId = 0;
            t1.VarBinaryField = new byte[] { 10, 20, 30, 40, 50, 60 };

            t1.Save();

            t1.BitField = false;
            t1.DateTimeField = DateTime.Now;
            t1.DecimalField = (decimal)5678.1234;
            t1.FloatField = (double)666.555;
            t1.GuidField = Guid.NewGuid();
            t1.ImageField = new byte[] { 11, 22, 33, 44, 55, 66 };
            t1.IntEnumField = IntEnum.Int2;
            t1.IntField = 99;
            t1.MoneyField = (decimal)99.99;
            t1.NTextField = "New NTextField";
            t1.NVarCharEnumField = NVarCharEnum.NVarChar2;
            t1.NVarCharField = "New NVarCharField";
            t1.RealField = (float)444.333;
            t1.VarBinaryField = new byte[] { 12, 23, 34, 45, 56, 67 };

            t1.Save();

            DataTypeEntity t2 = DataTypeEntity.GetEntity(t1.TableId);

            Assert.AreEqual(t1.BitField, t2.BitField);
            Assert.AreEqual(t1.DateTimeField.ToLongDateString(), t2.DateTimeField.ToLongDateString());
            Assert.AreEqual(t1.DecimalField, t2.DecimalField);
            Assert.AreEqual(t1.FloatField, t2.FloatField, 0.01);
            Assert.AreEqual(t1.ImageField[2], t2.ImageField[2]);
            Assert.AreEqual(t1.IntEnumField, t2.IntEnumField);
            Assert.AreEqual(t1.IntField, t2.IntField);
            Assert.AreEqual(t1.MoneyField, t2.MoneyField);
            Assert.AreEqual(t1.NTextField, t2.NTextField);
            Assert.AreEqual(t1.NVarCharEnumField, t2.NVarCharEnumField);
            Assert.AreEqual(t1.NVarCharField, t2.NVarCharField);
            Assert.AreEqual(t1.RealField, t2.RealField);
            Assert.AreEqual(t1.TableId, t2.TableId);
            Assert.AreEqual(t1.VarBinaryField[3], t2.VarBinaryField[3]);

        }

    }
}
